#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <pthread.h>

int fd;

// 闪烁线程函数
void* blink_thread(void* arg) {
    unsigned char databuf[1];
    int blink = 1;
    
    while(1) {
        databuf[0] = blink;
        write(fd, databuf, sizeof(databuf));
        blink = !blink;
        sleep(1);  // 1秒间隔
    }
    
    return NULL;
}

// 呼吸灯线程函数
void* breathe_thread(void* arg) {
    unsigned char databuf[1];
    int brightness = 0;
    int direction = 1;
    
    while(1) {
        // 模拟呼吸灯效果（通过快速开关灯来模拟）
        if(brightness == 0) {
            databuf[0] = 0;
            write(fd, databuf, sizeof(databuf));
            usleep(500000);  // 亮500ms
        } else {
            databuf[0] = 1;
            write(fd, databuf, sizeof(databuf));
            usleep(500000);  // 灭500ms
        }
        
        if(direction) {
            brightness++;
            if(brightness >= 1) {
                direction = 0;
            }
        } else {
            brightness--;
            if(brightness <= 0) {
                direction = 1;
            }
        }
    }
    
    return NULL;
}

int main(int argc, char *argv[]) {
    int retvalue;
    char *filename;
    unsigned char databuf[1];
    pthread_t thread_id;
    
    if(argc != 3) {
        printf("Error Usage!\r\n");
        printf("Usage: %s /dev/led <mode>\n", argv[0]);
        printf("mode: 0-Close LED, 1-Open LED, 2-Blink, 3-Breathe\n");
        return -1;
    }

    filename = argv[1];
    int mode = atoi(argv[2]);

    /* 打开 led 驱动 */
    fd = open(filename, O_RDWR);
    if(fd < 0) {
        printf("file %s open failed!\r\n", argv[1]);
        return -1;
    }

    switch(mode) {
        case 0:  // 关灯
            databuf[0] = 0;
            retvalue = write(fd, databuf, sizeof(databuf));
            if (retvalue < 0) {
                printf("LED Close Failed\r\n");
            } else {
                printf("LED Closed\r\n");
            }
            break;
            
        case 1:  // 开灯
            databuf[0] = 1;
            retvalue = write(fd, databuf, sizeof(databuf));
            if (retvalue < 0) {
                printf("LED Open Failed\r\n");
            } else {
                printf("LED Opened\r\n");
            }
            break;
            
        case 2:  // 闪烁
            printf("LED Blinking... (Press Ctrl+C to stop)\n");
            if(pthread_create(&thread_id, NULL, blink_thread, NULL) != 0) {
                printf("Failed to create blink thread\n");
                close(fd);
                return -1;
            }
            pthread_join(thread_id, NULL);
            break;
            
        case 3:  // 呼吸灯
            printf("LED Breathing... (Press Ctrl+C to stop)\n");
            if(pthread_create(&thread_id, NULL, breathe_thread, NULL) != 0) {
                printf("Failed to create breathe thread\n");
                close(fd);
                return -1;
            }
            pthread_join(thread_id, NULL);
            break;
            
        default:
            printf("Invalid mode! Please use 0-3\n");
            close(fd);
            return -1;
    }

    if(mode < 2) {  // 只有模式0和1需要关闭文件
        retvalue = close(fd);
        if(retvalue < 0) {
            printf("file %s close failed!\r\n", argv[1]);
            return -1;
        }
    }

    return 0;
}